#include "stdafx.h"
#include "FT2_Obj.h"

void FT2_Obj::InitFreeType(const char * fname, unsigned int h)
{
	FT_Error error = FT_Init_FreeType( &library );
	if ( error )
	{
		printf("... an error occurred during library initialization ...");
	}
	error = FT_New_Face( library, fname, 0, &face ); 

	if ( error == FT_Err_Unknown_File_Format )
	{
		printf("... the font file could be opened and read, but it appears\n... that its font format is unsupported");
	}
	else if ( error )
	{
		printf("... another error code means that the font file could not\n... be opened or read, or simply that it is broken...");
	}

	error = FT_Set_Char_Size(
		face,    /* handle to face object           */
		h << 6,       /* char_width in 1/64th of points  */
		h << 6,   /* char_height in 1/64th of points */
		72,     /* horizontal device resolution    */
		72 );   /* vertical device resolution      */

}
void FT2_Obj::DrawAUnicode(wchar_t ch) {
	if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
		printf("FT_Load_Glyph failed");
	FT_Glyph glyph;
	if(FT_Get_Glyph( face->glyph, &glyph ))
		printf("FT_Get_Glyph failed");
	FT_Render_Glyph( face->glyph,   FT_RENDER_MODE_MONO  ); 
	FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
	FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
	FT_Bitmap& bitmap=bitmap_glyph->bitmap;
	int width =  bitmap.width;
	int height =  bitmap.rows;

	wchar_t wstr[256];
	wsprintfW(wstr,L"World :%c , width :%d height : %d\n",ch,width , height);
	OutputDebugStringW(wstr);
	for(int j=0; j  < height ; j++){
		for(int i=0; i < width; i++){
			char tmp[5]={0};
			sprintf(tmp,"%2x",bitmap.buffer[i+ bitmap.width*j]);
			OutputDebugString(tmp);
		}
		OutputDebugString("\n");
	}
	OutputDebugString("\n");
}
void FT2_Obj::Free(){
	FT_Done_Face(face);
	FT_Done_FreeType(library);
}


textImage FT2_Obj::getImdata(wchar_t ch){
	if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
		printf("FT_Load_Glyph failed");
	FT_Glyph glyph;
	if(FT_Get_Glyph( face->glyph, &glyph ))
		printf("FT_Get_Glyph failed");
	FT_Render_Glyph( face->glyph,   FT_RENDER_MODE_MONO  ); 
	FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
	FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
	FT_Bitmap& bitmap=bitmap_glyph->bitmap;
	int width =  bitmap.width;
	int height =  bitmap.rows;

	textImage imdata;
	
	imdata.imWidth = width;
	imdata.imHeight = height;
	imdata.imData = new unsigned char[height * width *2];
	for(int j=0; j  < height ; j++){
		for(int i=0; i < width; i++){
			imdata.imData[i*2+ bitmap.width*j*2] = bitmap.buffer[i+ bitmap.width*j];
			imdata.imData[i*2+ bitmap.width*j*2+1] = bitmap.buffer[i+ bitmap.width*j];
		}
	}
	return imdata;
}
textImage FT2_Obj::getImdata(wchar_t ch, int imW, int imH){
	if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
		printf("FT_Load_Glyph failed");
	FT_Glyph glyph;
	if(FT_Get_Glyph( face->glyph, &glyph ))
		printf("FT_Get_Glyph failed");
	FT_Render_Glyph( face->glyph,   FT_RENDER_MODE_MONO  ); 
	FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
	FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
	FT_Bitmap& bitmap=bitmap_glyph->bitmap;
	int width =  bitmap.width;
	int height =  bitmap.rows;

	textImage imdata;

	imdata.imWidth = imW;
	imdata.imHeight = imH;
	imdata.imData = new unsigned char[imH * imW *2];
	for(int j=0; j < imH ; j++){
		for(int i=0; i < imW; i++){
			imdata.imData[i*2+ imW*j*2] = 0;
			imdata.imData[i*2+ imW*j*2+1] = 0;
		}
	}
	for(int j=0; j < height ; j++){
		for(int i=0; i < width; i++){
			imdata.imData[(i+(imW-width)/2)*2+ imW*(j+(imH-height)/2)*2] = bitmap.buffer[i+ bitmap.width*j];
			imdata.imData[(i+(imW-width)/2)*2+ imW*(j+(imH-height)/2)*2+1] = bitmap.buffer[i+ bitmap.width*j];
		}
	}
	return imdata;
}